package ru.ifmo.mlarinsky.classificator.model;

import java.util.*;

/**
 * @author Mikhail Larinskiy
 */
public class QuoteHistory implements Map<String, List<Quote>> {
    private final HashMap<String, List<Quote>> map = new HashMap<String, List<Quote>>();

    public int size() {
        return map.size();
    }

    public boolean isEmpty() {
        return map.isEmpty();
    }

    public boolean containsKey(Object key) {
        String upperCaseKey = keyToUpperCase(key);
        return upperCaseKey != null && map.containsKey(upperCaseKey);
    }

    public boolean containsValue(Object value) {
        return map.containsValue(value);
    }

    public List<Quote> get(Object key) {
        String upperCaseKey = keyToUpperCase(key);
        return map.get(upperCaseKey);
    }

    public List<Quote> put(String key, List<Quote> value) {
        String upperCaseKey = keyToUpperCase(key);
        return map.put(upperCaseKey, value);
    }

    public List<Quote> remove(Object key) {
        String upperCaseKey = keyToUpperCase(key);
        return map.remove(upperCaseKey);
    }

    public void putAll(Map<? extends String, ? extends List<Quote>> t) {
        Map<String, List<Quote>> upperCaseT = new HashMap<String, List<Quote>>();
        for (Entry<? extends String, ? extends List<Quote>> entry : t.entrySet()) {
            List<Quote> value = entry.getValue();
            String key = entry.getKey();
            upperCaseT.put(keyToUpperCase(key), value);
        }

        map.putAll(upperCaseT);
    }

    public void clear() {
        map.clear();
    }

    public Set<String> keySet() {
        return map.keySet();
    }

    public Collection<List<Quote>> values() {
        return map.values();
    }

    public Set<Entry<String, List<Quote>>> entrySet() {
        return map.entrySet();
    }

    private String keyToUpperCase(Object key) {
        if (!(key instanceof String))
            return null;
        return ((String) key).toUpperCase();
    }
}
